package com.zzzzzz.core.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zzzzzz.core.data.filestore.FileStorage;

@Controller
public class FileController {
	
	@Resource
	private FileStorage fileStorage;
	
	@RequestMapping(value = "/img/**", method = RequestMethod.GET)
	public void imageView(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String uri = request.getRequestURI();
		uri = uri.replace("/img/", "");
		
		response.setContentType("utf-8");
		InputStream is = fileStorage.getFile(uri);
		int read = 0;
		byte[] bytes = new byte[1024];
		OutputStream os = response.getOutputStream();
		while((read = is.read(bytes)) != -1) {
			os.write(bytes, 0, read);
		}
		os.flush();
		os.close();
	}
	
}
